package com.hacpai.android.md.presenter.implement;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.util.Log;

import com.hacpai.android.md.R;
import com.hacpai.android.md.model.api.ApiClient;
import com.hacpai.android.md.model.api.DefaultCallback;
import com.hacpai.android.md.model.api.ForegroundCallback;
import com.hacpai.android.md.model.entity.Article;
import com.hacpai.android.md.model.entity.Result;
import com.hacpai.android.md.model.entity.User;
import com.hacpai.android.md.presenter.contract.IUserDetailPresenter;
import com.hacpai.android.md.ui.adapter.UserDetailPagerAdapter;
import com.hacpai.android.md.ui.util.ActivityUtils;
import com.hacpai.android.md.ui.view.IUserDetailView;
import com.hacpai.android.md.util.HandlerUtils;

import java.util.List;

import okhttp3.Headers;

public class UserDetailPresenter implements IUserDetailPresenter {

    private final Activity activity;
    private final IUserDetailView userDetailView;

    private boolean loading = false;

    public UserDetailPresenter(@NonNull Activity activity, @NonNull IUserDetailView userDetailView) {
        this.activity = activity;
        this.userDetailView = userDetailView;
    }

    @Override
    public void getUserAsyncTask(@NonNull String loginName) {
        if (!loading) {
            loading = true;
            userDetailView.onGetUserStart();
            ApiClient.service.getUser(loginName).enqueue(new ForegroundCallback<Result.Data<Result.UserDate>>(activity) {

                private long startLoadingTime = System.currentTimeMillis();

                private long getPostTime() {
                    long postTime = 1000 - (System.currentTimeMillis() - startLoadingTime);
                    if (postTime > 0) {
                        return postTime;
                    } else {
                        return 0;
                    }
                }

                @Override
                public boolean onResultOk(int code, Headers headers, final Result.Data<Result.UserDate> result) {
                    HandlerUtils.postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            if (ActivityUtils.isAlive(getActivity())) {
                                userDetailView.onGetUserOk(result.getData().getUser());
                                onFinish();
                            }
                        }

                    }, getPostTime());
                    return true;
                }

                @Override
                public boolean onResultError(final int code, Headers headers, final Result.Error error) {
                    HandlerUtils.postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            if (ActivityUtils.isAlive(getActivity())) {
                                userDetailView.onGetUserError(code == 404 ? error.getErrorMessage() : getActivity().getString(R.string.data_load_faild_and_click_avatar_to_reload));
                                onFinish();
                            }
                        }

                    }, getPostTime());
                    return true;
                }

                @Override
                public boolean onCallException(Throwable t, Result.Error error) {
                    HandlerUtils.postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            if (ActivityUtils.isAlive(getActivity())) {
                                userDetailView.onGetUserError(getActivity().getString(R.string.data_load_faild_and_click_avatar_to_reload));
                                onFinish();
                            }
                        }

                    }, getPostTime());
                    return true;
                }

                @Override
                public void onFinish() {
                    userDetailView.onGetUserFinish();
                    loading = false;
                }

            });
            // TODO: 2017/3/18  Comments待定
//            ApiClient.service.getCommentTopicList(loginName , 1).enqueue(new DefaultCallback<Result.Data<Result.CommentData>>(activity) {
//
//                @Override
//                public boolean onResultOk(int code, Headers headers, Result.Data<Result.CommentData> result) {
//                    Log.d("xjf1128", "COMMENT::::"+result.getData().getComments());
//                    if (result.getData().getComments().size() > 0) {
//                        userDetailView.onGetTopicListOk(UserDetailPagerAdapter.COMMENT,result.getData().getComments());
//                    }
//                    return false;
//                }
//
//            });

            // TODO: 2017/3/18  API page加载
            ApiClient.service.getPostTopicList(loginName , 1).enqueue(new DefaultCallback<Result.Data<Result.ArticleData>>(activity) {

                @Override
                public boolean onResultOk(int code, Headers headers, Result.Data<Result.ArticleData> result) {
                    if (result.getData().getArticles().size() > 0) {
                        userDetailView.onGetTopicListOk(UserDetailPagerAdapter.POST,result.getData().getArticles());
                    }
                    return false;
                }

            });

            // TODO: 2017/3/18  API page加载
            ApiClient.service.getWatchTopicList(loginName , 1).enqueue(new DefaultCallback<Result.Data<Result.ArticleData>>(activity) {

                @Override
                public boolean onResultOk(int code, Headers headers, Result.Data<Result.ArticleData> result) {
                    if (result.getData().getArticles().size() > 0) {
                        userDetailView.onGetTopicListOk(UserDetailPagerAdapter.WATCH,result.getData().getArticles());
                    }
                    return false;
                }

            });
        }
    }

}
